package com.xj.api.bussiness.registerLicense.web;

import com.xj.api.bussiness.registerLicense.service.ITbRegisterLicenseService;
import com.xj.common.bussiness.registerLicense.entity.registerLicenseMould;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.api.base.web.BaseController;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.registerLicense.entity.TbRegisterLicense;

import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Hmemb
 * @since 2018-01-12
 */
@RestController
@RequestMapping("registerLicense")
public class TbRegisterLicenseController extends BaseController {

	@Autowired
	private ITbRegisterLicenseService registerLicenseService;

	@GetMapping
	@ApiOperation(value = "  列表", httpMethod = "GET", notes = "  列表")
	public AbstractBean registerLicenseList(@RequestParam(required = false, defaultValue = "0") Integer pageNum, @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		return json(registerLicenseService.selectMapsPage(new Page<TbRegisterLicense>(pageNum, pageSize), new EntityWrapper<TbRegisterLicense>()));
	}

	@GetMapping("/{id}.auth")
	@ApiOperation(value = "  詳情", httpMethod = "GET", notes = "  詳情")
	public AbstractBean registerLicenseContent(@PathVariable Integer id) {
		return json(registerLicenseService.searchRegisterLicenseAndImg(id));
	}

	@PostMapping("save")
	@ApiOperation(value = "  新增", httpMethod = "POST", notes = " 新增")
	public AbstractBean add(TbRegisterLicense tbregisterlicense) {
		tbregisterlicense.setCreateDate(new Date());
		if (!registerLicenseService.insertOrUpdate(tbregisterlicense)) {
			return error();
		}
		return success();
	}

	@DeleteMapping("{id}/delete")
	@ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
	public AbstractBean delete(@PathVariable(required = true) Integer id) {
		if (!registerLicenseService.deleteById(id)) {
			return error();
		}
		return success();
	}

	@GetMapping("searchTransforRegister")
	@ApiOperation(value = " 查询设备上户 ", httpMethod = "GET", notes = " 查询设备上户")
	public AbstractBean searchTransforRegister(TbRegisterLicense registerlicense) {
		if (registerLicenseService.searchTransferForRegister(registerlicense) != null) {
			return json(registerLicenseService.searchTransferForRegister(registerlicense));
		}
		return super.error();
	}

	@GetMapping("searchRegisterLicenseList")
	@ApiOperation(value = " 查询设备所属人上户表的所有信息 ", httpMethod = "GET", notes = " 查询设备所属人上户表的所有信息")
	public AbstractBean searchRegisterLicenseList(TbRegisterLicense registerlicense) {
		if (registerLicenseService.searchRegisterLicenseList(registerlicense) != null) {
			return json(registerLicenseService.searchRegisterLicenseList(registerlicense));
		}
		return super.error();
	}

	@GetMapping("selectTransforRegister")
	@ApiOperation(value = " 查询设备上户信息状态 ", httpMethod = "GET", notes = " 查询设备上户信息的状态")
	public AbstractBean selectTransforRegister(TbRegisterLicense registerlicense) {
		List<registerLicenseMould> list = registerLicenseService.searchRegisterLicenseList(registerlicense);
		if (list != null && list.size()>0){
			return SuccessMessage("存在","1");
		}else {
			return fail("不存在","0");
		}
	}
}
